package com.bd;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class VetsModule {
       
	public final static String TABLE_NAME = "VetsModule";
	public final static String COL_PK = "id";
	public final static String COL_PET_ID = "petid";
    public final static String COL_VETNAME = "vetname";
    public final static String COL_VETPHONE = "vetphone";
    public final static String COL_VETADDR = "vetaddr";
    
    public final static String[] ALL_COLS =  {
    	VetsModule.COL_PK, 
    	VetsModule.COL_PET_ID, 
    	VetsModule.COL_VETNAME,
    	VetsModule.COL_VETPHONE,
    	VetsModule.COL_VETADDR
    	};
    
    private int pk = -1;
    private String petid = "";
    private String vetname = "";
    private String vetphone = "";
    private String vetaddr = "";       
       
    public VetsModule(String petid, String vetname, String vetphone, String vetaddr){
           
    	this.petid = petid;
        this.vetname = vetname;
        this.vetphone = vetphone;
        this.vetaddr = vetaddr;           
    }
    
    
       
    private VetsModule(){}
       
       

    @Override
    public String toString() {
        return "Vet [pk=" + this.pk + ", petid=" + this.petid + ", vetname=" + this.vetname+ ", vetphone=" + this.vetphone+ ", vetaddr=" + this.vetaddr+ "]";
    }
       
    /***************************************************************************
     * SETTERS AND GETTERS
     **************************************************************************/
    
    public String getPetid() {
		return petid;
	}

	public void setPetid(String petid) {
		this.petid = petid;
	}

	public String getVetname() {
		return vetname;
	}

	public void setVetname(String vetname) {
		this.vetname = vetname;
	}

	public String getVetphone() {
		return vetphone;
	}

	public void setVetphone(String vetphone) {
		this.vetphone = vetphone;
	}

	public String getVetaddr() {
		return vetaddr;
	}

	public void setVetaddr(String vetaddr) {
		this.vetaddr = vetaddr;
	}
	

	public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }

    

    /***************************************************************************
     * createTableString
     **************************************************************************/
    public static String createTableString(){
        String create = "CREATE TABLE " + VetsModule.TABLE_NAME + "(";
        create += VetsModule.COL_PK + " INTEGER PRIMARY KEY, ";
        create += VetsModule.COL_PET_ID + " STRING, ";
        create += VetsModule.COL_VETNAME + " STRING, ";
        create += VetsModule.COL_VETPHONE + " STRING, ";
        create += VetsModule.COL_VETADDR + " STRING";
   
        create += ");";
       
        return create;
    }
   
   
    /***************************************************************************
     * insertModule
     **************************************************************************/
    public VetsModule insertVet(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(VetsModule.COL_PET_ID,this.getPetid());
	    values.put(VetsModule.COL_VETNAME,this.getVetname());
	    values.put(VetsModule.COL_VETPHONE,this.getVetphone());
	    values.put(VetsModule.COL_VETADDR,this.getVetaddr());
	   
	    long insertId = database.insert(VetsModule.TABLE_NAME, null, values);
	   
	    return VetsModule.getVet(database, insertId);
    }
       
    /***************************************************************************
     * updateModule
     **************************************************************************/
    public boolean updateVet(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(VetsModule.COL_PET_ID,this.getPetid());
	    values.put(VetsModule.COL_VETNAME,this.getVetname());
	    values.put(VetsModule.COL_VETPHONE,this.getVetphone());
	    values.put(VetsModule.COL_VETADDR,this.getVetaddr());
       
	    String whereClause = VetsModule.COL_PK + "= \"" + this.pk + "\"";
       
        int affected = database.update(VetsModule.TABLE_NAME, values,
                whereClause, null);
       
        if(affected==0){
            return false;
        }
        return true;
    }
       
    /***************************************************************************
     * getModule
     **************************************************************************/
    public static VetsModule getVet(SQLiteDatabase database, long insertId) {
       
        String selection = VetsModule.COL_PK + "=" + insertId;
       
        Cursor cursor = database.query(VetsModule.TABLE_NAME, VetsModule.ALL_COLS,
                        selection, null, null, null, null);
       
        cursor.moveToFirst();
        VetsModule module = VetsModule.cursorToVet(cursor);
        cursor.close();
        return module;
    }
       
    public static VetsModule getVet(SQLiteDatabase database, String id) {
       
        String selection = VetsModule.COL_PK + " = \"" + id + "\"";
       
        Cursor cursor = database.query(VetsModule.TABLE_NAME, VetsModule.ALL_COLS,
                selection, null, null, null, null);
       
        cursor.moveToFirst();
        VetsModule module = VetsModule.cursorToVet(cursor);
        cursor.close();
        return module;
    }
    /***************************************************************************
     * deleteModule
     **************************************************************************/
    public void deleteVet(SQLiteDatabase database){
       
    	 //delete from the Module table
        database.delete(VetsModule.TABLE_NAME, VetsModule.COL_PK + "=" + this.pk, null);
        System.out.println("Vet deleted: " + this.toString());
    }
       
    /***************************************************************************
     * cursorToModule
     **************************************************************************/
    private static VetsModule cursorToVet(Cursor cursor) {
       
        VetsModule Module = new VetsModule();
        Module.setPk(cursor.getInt(0));
        Module.setPetid(cursor.getString(1));
        Module.setVetname(cursor.getString(2));
        Module.setVetphone(cursor.getString(3));
        Module.setVetaddr(cursor.getString(4));
        return Module;
    }
       
        /***************************************************************************
     * getAllModules
     **************************************************************************/
        public static List<VetsModule> getAllVets(SQLiteDatabase database) {
        List<VetsModule> modules = new ArrayList<VetsModule>();
       
        Cursor cursor = database.query(VetsModule.TABLE_NAME, VetsModule.ALL_COLS, null,
                null, null, null, null);
       
        if(cursor.getCount()==0){
            cursor.close();
            return modules;
        }
       
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            VetsModule module = VetsModule.cursorToVet(cursor);
            modules.add(module);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return modules;
    }
        
       
}
